using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Patient
/// </summary>
public class Patient
{
    DataSet ds = new DataSet();
    Manager mgr = new Manager(true,"ConnectionString");
    private int iPatientId=0;
    private string  sfname="";
    private string sMName="";
    private string sLName = "";
    private string sBirthYear = "";
    private string sBirthDate = "";
    private string sPassword = "";
    private string sGender = "";
    private string sAddress1 = "";
    private string sAddress2 = "";
    private string sCity = "";
    private string sZipCode = "";
    private string sState = "";
    private string sSecurityQues = "";
    private string sSecurityAns = "";
    private string sNickname = "";
    private string sFreeoffer = "";
    //private string sEmail;
    private string sCountryid = "";
    private string sHealthCPlan = "";
    private string sRegdate = "";
    private bool sIsDelete = true;

	public Patient()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    //Property
    public int PatientId
    {
        get { return iPatientId; }
        set { iPatientId = value; }

    }
    public string FirstName
    {
        get { return sfname; }
        set { sfname = value; }
    }
    public string MiddleName
    {
        get { return sMName; }
        set { sMName = value; }
    }
    public string LastName
    {
        get { return sLName; }
        set { sLName = value; }
    }
    public string BirthYear
    {
        get { return sBirthYear; }
        set { sBirthYear = value; }
    }
    public string BirthDate
    {
        get { return sBirthDate; }
        set { sBirthDate = value; }
    }
    //public string Email
    //{
    //    get { return sEmail; }
    //    set { sEmail = value; }
    //}
    public string Password
    {
        get { return sPassword; }
        set { sPassword = value; }
    }
    public string Gender
    {
        get { return sGender; }
        set { sGender = value; }
    }
    public string Address1
    {
        get { return sAddress1; }
        set { sAddress1 = value; }
    }
    public string Address2
    {
        get { return sAddress2; }
        set { sAddress2 = value; }
    }
    public string City
    {
        get { return sCity; }
        set { sCity = value; }
    }
    public string ZipCode
    {
        get { return sZipCode; }
        set { sZipCode = value; }
    }
    public string State
    {
        get { return sState; }
        set { sState = value; }
    }
    public string SecurityQues
    {
        get { return sSecurityQues; }
        set { sSecurityQues = value; }
    }
    public string SecurityAns
    {
        get { return sSecurityAns; }
        set { sSecurityAns = value; }
    }
    public string NickName
    {
        get { return sNickname; }
        set { sNickname = value; }
    }
    public string FreeOffer
    {
        get { return sFreeoffer; }
        set { sFreeoffer = value; }
    }
    public string Country
    {
        get { return sCountryid; }
        set { sCountryid = value; }
    }
    public string HealthCPlan
    {
        get { return sHealthCPlan; }
        set { sHealthCPlan = value; }
    }
    public string RegDate
    {
        get { return sRegdate; }
        set { sRegdate = value; }
    }
    public bool IsDelete
    {
        get { return sIsDelete; }
        set { sIsDelete= value; }
    }

    //
    public DataSet FillUserInfoByUserID()
    {
        
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[1];

            sqlParam[0] = new SqlParameter("@id", iPatientId);
          DataSet  ds = mgr.ExecuteQuery("UserInfoByUserID", sqlParam, "tblUsers");

            return ds;
        }
        catch (Exception ex)
        {
            throw (ex);
        }

    }
    public int UpdateUserInfo()
    {
        int iResult = 0;
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[21];
            sqlParam[0] = new SqlParameter("@id", iPatientId);
            sqlParam[1] = new SqlParameter("@fname", sfname);
            sqlParam[2] = new SqlParameter("@MName", sMName);
            sqlParam[3] = new SqlParameter("@LName", sLName);
            sqlParam[4] = new SqlParameter("@BirthYear", sBirthYear);
            sqlParam[5] = new SqlParameter("@BirthDate", sBirthDate);
            sqlParam[6] = new SqlParameter("@Password", sPassword);
            sqlParam[7] = new SqlParameter("@Gender", sGender);
            sqlParam[8] = new SqlParameter("@Address1", sAddress1);
            sqlParam[9] = new SqlParameter("@Address2", sAddress2);
            sqlParam[10] = new SqlParameter("@City", sCity);
            sqlParam[11] = new SqlParameter("@ZipCode", sZipCode);
            sqlParam[12] = new SqlParameter("@State", sState);
            sqlParam[13] = new SqlParameter("@SecurityQues", sSecurityQues);
            sqlParam[14] = new SqlParameter("@SecurityAns", sSecurityAns);
            sqlParam[15] = new SqlParameter("@Nickname", sNickname);
            sqlParam[16] = new SqlParameter("@Freeoffer", sFreeoffer);
            sqlParam[17] = new SqlParameter("@Countryid", sCountryid);
            sqlParam[18] = new SqlParameter("@HealthCPlan", sHealthCPlan);
            sqlParam[19] = new SqlParameter("@Regdate", sRegdate);
            sqlParam[20] = new SqlParameter("@Result", SqlDbType.BigInt);
            sqlParam[20].Direction = ParameterDirection.Output;

            mgr.ExecuteUpdate("UpdateUserInfo", sqlParam);

            return Convert.ToInt32(sqlParam[20].Value);
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message.ToString());
        }
        return iResult;
    }

    /// <summary>
    /// Authenticate the credentials provided for a user login
    /// Author: 
    /// Implemented By: Deepak
    /// Date: 18th Feb 2008
    /// </summary>
    /// <param name="email">Email of Site User</param>
    /// <param name="pass">Password</param>
    /// <param name="extraPath">extraPath is obsolete in current scenario, pass 'user/' or empty string</param>
    /// <returns>Error message if login False, else redirects to the home page</returns>
    /// <remarks>
    /// Following session varibles are set on authentication:
    /// userID, LoginName, UserEmail
    /// userType = "SiteUser"
    /// DoctorType = "User"
    /// </remarks>
    public string DoPatientLogin(string email, string pass, string extraPath)
    {
        if (email.Trim().Length == 0 || pass.Length == 0)
        {
            return "Invalid Login";
        }

        Manager objManager = new Manager(true,"ConnectionString");
        string strSql = "Select ID,fName,Mname,LName,Password FROM tbl_Users where Email='" + objManager.FixQuote(email) + "' and isdelete=0";
        SqlDataReader sqlDr = objManager.ExecuteQuery(strSql);
        if (sqlDr.Read())
        {
            if (string.Compare(sqlDr["password"].ToString(), pass, false) == 0)
            {
                HttpContext.Current.Session["userID"] = sqlDr["id"].ToString();
                HttpContext.Current.Session["userType"] = "SiteUser";
                HttpContext.Current.Session["LoginName"] = sqlDr["fName"].ToString() + " " + sqlDr["Mname"].ToString() + " " + sqlDr["LName"].ToString();
                HttpContext.Current.Session["UserEmail"] = email;
                HttpContext.Current.Session["DoctorType"] = "User";

                sqlDr.Close();

                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["fromPg"]))
                    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.QueryString["fromPg"]);
                else
                    if (extraPath != "NOREDIRECT")
                    {
                        HttpContext.Current.Response.Redirect(HttpContext.Current.Session["strHttp"].ToString());
                    }
            }
            else
            {
                sqlDr.Close();
                return "Your password is incorrect. Please try again.";
            }
        }
        else
        {
            sqlDr.Close();
            return "Your login information is incorrect. Please try again.";
        }
        return "";
    }
}
